package com.example.flashsales.controller;

import com.example.flashsales.Tools.NumberUtil;
import com.example.flashsales.Tools.TokenTool;
import com.example.flashsales.entity.Result;
import com.example.flashsales.entity.dto.AdminDto;
import com.example.flashsales.entity.dto.RefundDto;
import com.example.flashsales.entity.dto.StockDto;
import com.example.flashsales.entity.dto.StoreDto;
import com.example.flashsales.entity.pojo.Admin;
import com.example.flashsales.entity.pojo.OrderDetails;
import com.example.flashsales.entity.pojo.Store;
import com.example.flashsales.entity.vo.*;
import com.example.flashsales.role.role;
import com.example.flashsales.service.impl.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理人员的接口 商品的管理等
 */
@Controller
@CrossOrigin
@RequestMapping("/admin")
public class AdminController {


    @Autowired
    StoreCostRecordServiceImpl storeCostRecordService;

    @Autowired
    SortServiceImpl sortService;

    @Autowired
    StoreServiceImpl storeService;

    @Autowired
    OrderServiceImpl orderService;

    @Autowired
    GoodsServiceImpl goodsService;

    @Autowired
    AdminServiceImpl adminService;

    @Autowired
    RefundServiceImpl refundService;

    @Autowired
    StockServiceImpl stockService;

    @Autowired
    UserServiceImpl userService;

    @Autowired
    AdminCostRecordServiceImpl adminCostRecordService;

    @Autowired
    RoleServiceImpl roleService;


    /**
     * 管理人员登录
     *
     * @param httpServletRequest
     * @param httpServletResponse
     * @param adminDto
     * @return
     */
    @ResponseBody
    @PostMapping("/login")
    public Result login(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, @RequestBody AdminDto adminDto) {
        httpServletResponse.setHeader("Access-Control-Allow-Headers", "x-requested-with,Content-Type");
        httpServletResponse.setHeader("Access-Control-Allow-Origin", httpServletRequest.getHeader("origin"));
        httpServletResponse.setHeader("Access-Control-Allow-Methods", "POST,GET");
        httpServletResponse.addHeader("Access-Control-Expose-Headers", "Token");
        if (adminDto.getPassword() == null || adminDto.getAccount() == null || adminDto.getAccount().equals("") || adminDto.getPassword().equals("")) {
            return Result.error("账号密码不能为空");
        }
        final AdminVo login = adminService.login(adminDto.getPassword(), adminDto.getAccount());
        final Admin admin = roleService.viewAdminByAccount(login.getAccount());
        if (admin.getS() == 1){
            return Result.error("你已被封号");
        }
        if (login == null) {
            return Result.error("账号密码错误");
        }
        //生成Token
        Map<String, String> map = new HashMap<>();
        map.put("id", login.getId() + "");
        map.put("state", login.getState() + "");
        final String token = TokenTool.createToken(map);
        httpServletResponse.setHeader("Token", token);
        return Result.ok("登录成功").setData(login);
    }


    /**
     * 设置店家拉一个人挣的金额
     *
     * @param storeId
     * @param money
     * @return
     */
    @role(role = 1)
    @ResponseBody
    @GetMapping("/updateStoreUserMoney")
    public Result updateStoreUserMoney(HttpServletRequest httpServletRequest,Long storeId, Double money) {
        if (storeId == null || money == null){
            return Result.error("给我传空我打死你");
        }
        if (money < 0) {
            return Result.error("金额不能小于0");
        }
        final boolean b = adminService.updateStoreUserMoney(storeId, money);
        if (b) {
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }


    /**
     * 修改店家成交一单的金额
     *
     * @param storeId
     * @param money
     * @return
     */
    @role(role = 2)
    @ResponseBody
    @GetMapping("/updateStoreOrderMoney")
    public Result updateStoreOrderMoney(HttpServletRequest httpServletRequest,Long storeId, Double money) {
        if (storeId == null || money == null){
            return Result.error("给我传空我打死你");
        }
        if (money < 0) {
            return Result.error("金额不能小于0");
        }
        final boolean b = adminService.updateStoreOrderMoney(storeId, money);
        if (b) {
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }


    /**
     * 查看所有店家
     *
     * @return
     */
    @role(role=3)
    @ResponseBody
    @GetMapping("/viewStore")
    public Result viewStore(HttpServletRequest httpServletRequest,Integer page,Integer num) {
        if (page == null || num == null){
            return Result.error("数据不能为空");
        }
        if (page <= 0){
            return Result.error("页数不能小于1");
        }

        final PageInfo<Store> storePageInfo = adminService.viewStore(page, num);
        if (storePageInfo == null) {
            return Result.error("查看失败");
        }
        return Result.ok("查看成功").setData(storePageInfo);
    }


    /**
     * 查看店家信息
     *
     * @param storeId
     * @return
     */
    @ResponseBody
    @role(role = 4)
    @GetMapping("/viewStoreMessage")
    public Result viewStoreMessage(HttpServletRequest httpServletRequest,Long storeId) {
        if (storeId == null){
            return Result.error("给我传空我打死你");
        }
        final StoreVo storeVo = adminService.viewStoreMessage(storeId);
        if (storeVo == null) {
            return Result.error("查看失败");
        }
        return Result.ok("查看成功").setData(storeVo);
    }


    /**
     * 修改店家状态
     *
     * @param storeId
     * @param state   (0 正常 1 非法)
     * @return
     */
    @role(role = 5)
    @ResponseBody
    @GetMapping("/updateStoreState")
    public Result updateStoreState(HttpServletRequest httpServletRequest,Long storeId, Integer state) {
        if (state == null || storeId == null){
            return Result.error("给我传空我打死你");
        }
        if (state != 0 && state != 1) {
            return Result.error("状态不合法");
        }
        final boolean b = adminService.updateStoreState(storeId, state);
        if (b) {
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }


    /**
     * 修改每天订单的截止时间
     *
     * @param date
     * @return
     */
    @role(role = 6)
    @ResponseBody
    @GetMapping("/updateTimeConfig")
    public Result updateTimeConfig(HttpServletRequest httpServletRequest,Date date) {
        if (date == null){
            return Result.error("给我传空我打死你");
        }
        if (date == null) {
            return Result.error("时间不能为空");
        }
        final boolean b = adminService.updateTimeConfig(date);
        if (b) {
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }


//    /**
//     * 修改退货订单状态
//     *
//     * @param state
//     * @param refundId
//     * @param httpServletRequest
//     * @return
//     */
//    @GetMapping("/updateRefundState")
//    @ResponseBody
//    public Result updateRefundState(Integer state, Long refundId, HttpServletRequest httpServletRequest) {
//        if (state == null || refundId == null){
//            return Result.error("给我传空我打死你");
//        }
//        final int id = Integer.parseInt(httpServletRequest.getAttribute("id") + "");
//        if (state != 0 && state != 1 && state != 2) {
//            return Result.error("修改失败，状态不合法");
//        }
//        final String s = adminService.updateRefundState(state, refundId, id);
//        if (s.equals("处理完成")) {
//            return Result.ok(s);
//        }
//        return Result.error(s);
//    }


    /**
     * 修改退货订单细节状态
     *
     * @param state
     * @param detailsId
     * @return
     */
    @role(role = 7)
    @ResponseBody
    @GetMapping("/updateRefundDetailsState")
    public Result updateRefundDetailsState(HttpServletRequest httpServletRequest,Integer state, Long detailsId) {
        if (state == null || detailsId == null){
            return Result.error("给我传空我打死你");
        }
        final Object id = httpServletRequest.getAttribute("id");
        final int adminId = Integer.parseInt(id + "");
        if (state != 0 && state != 1) {
            return Result.error("修改失败，状态不合法");
        }
        final boolean b = adminService.updateRefundDetailsState(state, detailsId,adminId);
        if (b) {
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }


    /**
     * 按类型查退款订单
     *
     * @param state
     * @return
     */
    @role(role = 8)
    @GetMapping("/viewRefundByState")
    @ResponseBody
    public Result viewRefundByState(HttpServletRequest httpServletRequest,Integer state,Integer page,Integer num) {
        if (state == null || page == null || num == null){
            return Result.error("给我传空我打死你");
        }
        if (page <=0 ){
            return Result.error("页数不能小于1");
        }
        final List<ReturnOrderVo> returnOrderVos = adminService.viewRefundByState(state,page,num);
        Map<Object,Object> map = new HashMap<>();
        map.put("total",refundService.viewCountByState(state));
        map.put("message",returnOrderVos);
        return Result.ok("查看成功").setData(map);
    }


    /**
     * 查看所有退款订单
     *
     * @return
     */
    @role(role = 9)
    @ResponseBody
    @GetMapping("/viewAllRefund")
    public Result viewAllRefund(HttpServletRequest httpServletRequest,Integer num , Integer page) {
        if (page == null || num == null){
            return Result.error("给我传空我打死你");
        }
        if (page <=0 ){
            return Result.error("页数不能小于1");
        }
        PageHelper.startPage(page,num);
        final List<ReturnOrderVo> returnOrderVos = adminService.viewAllRefund(page,num);
        Map<Object,Object> map = new HashMap<>();
        map.put("total",refundService.viewCount());
        map.put("message",new PageInfo<>(returnOrderVos).getList());
        return Result.ok("查看成功").setData(map);
    }


    /**
     * 查看某个退后订单的详情
     *
     * @param refundId
     * @return
     */
    @role(role = 10)
    @ResponseBody
    @GetMapping("/viewRefundDetails")
    public Result viewRefundDetails(HttpServletRequest httpServletRequest,Long refundId) {
        if (refundId == null){
            return Result.error("给我传空我打死你");

        }
        final ReturnOrderVo returnOrderVo = adminService.viewRefundDetails(refundId);
        if (returnOrderVo == null) {
            return Result.error("查看失败");
        }
        return Result.ok("查看成功").setData(returnOrderVo);
    }


    /**
     * 店家的注册
     *
     * @param storeDto
     * @return
     */
    @role(role = 11)
    @PostMapping("/registerStore")
    @ResponseBody
    public Result registerStore(HttpServletRequest httpServletRequest,@RequestBody StoreDto storeDto) {
        if (storeDto == null || storeDto.getPhone() == null || storeDto.getPassword() == null || storeDto.getAddress() == null || storeDto.getName() == null) {
            return Result.error("信息不能为空");
        }
        if(!NumberUtil.isPhone(storeDto.getPhone())){
            return Result.error("请输入正确的电话号码");
        }
        storeDto.setInviteCode(storeDto.getPhone() + "G");
        final String s = adminService.registerStore(storeDto);
        Map<String, String> map = new HashMap<>();
        map.put("邀请码", storeDto.getInviteCode());
        if (s.equals("注册成功")) {
            return Result.ok(s).setData(map);
        }
        return Result.error(s);
    }


//    /**
//     * 分拣的时候退款
//     *
//     * @param refundDto
//     * @return
//     */
//    @role(role = 3)
//    @ResponseBody
//    @PostMapping("/addReturnRecord")
//    public Result addReturnRecord(HttpServletRequest httpServletRequest,@RequestBody RefundDto refundDto) {
//        final int id = Integer.parseInt(httpServletRequest.getAttribute("id") + "");
//        if (refundDto == null || refundDto.getRefundDetailsDtoList() == null || refundDto.getRefundDetailsDtoList().size() == 0 || refundDto.getOrderId() == 0) {
//            return Result.error("数据不能为空");
//        }
//        refundDto.setAdminId(id);
//        final boolean b = adminService.addReturnRecord(refundDto, refundDto.getRefundDetailsDtoList());
//        if (b) {
//            return Result.ok("添加成功");
//        }
//        return Result.error("添加失败");
//    }


    /**
     * 根据时间查看店家的订单
     *
     * @param page
     * @param num
     * @param qDate
     * @param hDate
     * @param storeId
     * @return
     */
    @role(role = 12)
    @ResponseBody
    @GetMapping("/viewOrdersByTime")
    public Result viewOrdersByTime(HttpServletRequest httpServletRequest,Integer page, Integer num, Date qDate, Date hDate, Long storeId) {
        if (page == null || num == null || hDate == null || qDate == null || storeId == null){
            return Result.error("给我传空我打死你");
        }
        if (page <= 0) {
            return Result.error("页数大于等于1");
        }
        final List<OrdersVo> ordersVoList = adminService.viewOrdersByTime(storeId, page, num, qDate, hDate);
        if (ordersVoList == null) {
            return Result.error("查看失败，没有数据");
        }
        Map<Object,Object> map = new HashMap<>();
        map.put("total",orderService.viewStoreOrderCountByTime(storeId,qDate,hDate));
        map.put("message",ordersVoList);
        return Result.ok("查看成功").setData(map);
    }


    /**
     * 查看店家的所有订单
     *
     * @param storeId
     * @param num
     * @param page
     * @return
     */
    @ResponseBody
    @role(role = 13)
    @GetMapping("/viewAllOrderByStore")
    public Result viewAllOrderByStore(HttpServletRequest httpServletRequest,Long storeId, Integer num, Integer page) {
        if (storeId == null || num == null || page == null){
            return Result.error("给我传空我打死你");
        }
        if (page <= 0) {
            return Result.error("页数大于等于1");
        }
        final List<OrdersVo> ordersVoList = adminService.viewAllOrderByStore(storeId, page, num);
        if (ordersVoList == null) {
            return Result.error("查看失败，没有数据");
        }
        Map<Object,Object> map = new HashMap<>();
        map.put("total",orderService.viewStoreOrderCount(storeId));
        map.put("message",ordersVoList);
        return Result.ok("查看成功").setData(map);
    }


    /**
     * 根据重量查看订单细节
     * @param goodsId
     * @param count
     * @return
     */
    @role(role = 14)
    @ResponseBody
    @GetMapping("/viewOrderDetailsVo")
    public Result viewOrderDetailsVo(HttpServletRequest httpServletRequest,Long goodsId,Double count,Integer page,Integer num){
        if (goodsId == null || count == null || page == null || num == null || page <= 0 || num < 0){
            return Result.error("给我传空我打死你");

        }
        if (count <= 0){
            return Result.error("查看失败,数量不能小于0");
        }
        PageHelper.startPage(page,num);
        final List<OrderDetails> orderDetails = adminService.viewOrderDetailsVo(count, goodsId);
        if (orderDetails == null){
            return Result.error("查看失败");
        }
        return Result.ok("查看成功").setData(new PageInfo<>(orderDetails));
    }


    /**
     * TODO 查看所有未分拣的订单
     * @param httpServletRequest
     * @param page
     * @param num
     * @return
     */
    @role(role = 15)
    @ResponseBody
    @GetMapping("/viewAllDetails")
    public Result viewAllDetails(HttpServletRequest httpServletRequest,Integer page,Integer num){
        if (page == null || num == null || page <= 0 || num < 0){
            return Result.error("数据异常");
        }
        PageHelper.startPage(page,num);
        final List<OrderDetails> orderDetails = orderService.viewAllDetails(page, num);
        return Result.ok("查看成功").setData(new PageInfo<>(orderDetails));
    }



    /**
     * 修改某个订单中某个商品的分拣状态
     *
     * @param httpServletRequest
     * @param detailsId          细节id
     * @param stockId
     * @return
     */
    @role(role = 16)
    @ResponseBody
    @GetMapping("/updateOrderDetailsState")
    public Result updateOrderDetailsState(HttpServletRequest httpServletRequest, Long detailsId, Long stockId,Double count) {
        if (detailsId == null || stockId == null){
            return Result.error("给我传空我打死你");
        }
        final int id = Integer.parseInt(httpServletRequest.getAttribute("id") + "");
        final boolean b = adminService.updateOrderDetailsState(detailsId, id, stockId,count);
        if (b) {
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }


    /**
     * 查看店家的拉人情况
     *
     * @param storeId
     * @return
     */
    @role(role = 17)
    @ResponseBody
    @GetMapping("/viewStoreUser")
    public Result viewStoreUser(HttpServletRequest httpServletRequest,Long storeId) {
        final List<UserVo> userVos = adminService.viewStoreUser(storeId);
        if (userVos == null) {
            return Result.error("查看失败");
        }
        return Result.ok("查看成功").setData(userVos);
    }


    /**
     * 按类型查看店家
     *
     * @param state （0 正常 1非法）
     * @return
     */
    @role(role = 18)
    @ResponseBody
    @GetMapping("/viewStoreByState")
    public Result viewStoreByState(HttpServletRequest httpServletRequest,Integer state) {
        if (state != 0 && state != 1) {
            return Result.error("状态异常");
        }
        final List<StoreVo> storeVos = adminService.viewStoreByState(state);
        if (storeVos == null) {
            return Result.error("查看失败");
        }
        return Result.ok("查看成功").setData(storeVos);
    }


    /**
     * 根据用户的电话号码查询订单（电话号码加时间）
     *
     * @param phone
     * @param qDate
     * @param hDate
     * @param page
     * @param num
     * @return
     */
    @role(role = 19)
    @ResponseBody
    @GetMapping("/viewOrdersByUserPhoneAndTime")
    public Result viewOrdersByUserPhoneAndTime(HttpServletRequest httpServletRequest,String phone, Date qDate, Date hDate, Integer page, Integer num) {
        if (phone == null || qDate == null || hDate == null || page == null || num == null){
            return Result.error("给我传空我打死你");
        }
        if (!NumberUtil.isPhone(phone)) {
            return Result.error("请输入正确的手机号");
        }
        if (page <= 0) {
            return Result.error("页数不能小于1");
        }
        final List<OrdersVo> ordersVoList = adminService.viewOrdersByUserPhoneAndTime(phone, qDate, hDate, page, num);
        if (ordersVoList == null) {
            return Result.error("查看失败");
        }
        PageHelper.startPage(page,num);
        return Result.ok("查看成功").setData(new PageInfo<>(ordersVoList));
    }


//    /**
//     * 打印所有流水
//     *
//     * @return
//     */
////    @role(role = 2)
//    @ResponseBody
//    @GetMapping("/viewAllCostRecord")
//    public Result viewAllCostRecord(HttpServletRequest httpServletRequest,Integer page, Integer num) {
//        if (page == null || num == null){
//            return Result.error("请输入参数");
//
//        }
//        if (page <= 0) {
//            return Result.error("页数不能小于1");
//        }
//        final List<AdminCostRecordVo> adminCostRecordVos = adminService.viewAllCostRecord(page, num);
//        if (adminCostRecordVos == null) {
//            return Result.error("查看失败");
//        }
//        PageHelper.startPage(page,num);
//        return Result.ok("查看成功").setData(new PageInfo<>(adminCostRecordVos));
//    }


//    /**
//     * 打印流水（可按天，也可按月）
//     *
//     * @param qTime
//     * @param page
//     * @param num
//     * @param hTime
//     * @return
//     */
//    @role(role = 2)
//    @ResponseBody
//    @GetMapping("/viewCostRecord")
//    public Result viewCostRecord(HttpServletRequest httpServletRequest,Date qTime, Integer page, Integer num, Date hTime) {
//        if (qTime == null || page == null || num == null || hTime == null){
//            return Result.error("请输入正确的手机号");
//
//        }
//        if (page <= 0) {
//            return Result.error("页数不能小于1");
//        }
//        final List<AdminCostRecordVo> adminCostRecordVos = adminService.viewCostRecord(qTime, page, num, hTime);
//        if (adminCostRecordVos == null) {
//            return Result.error("查看失败");
//        }
//        PageHelper.startPage(page,num);
//        return Result.ok("查看成功").setData(new PageInfo<>(adminCostRecordVos));
//    }


    /**
     * 打印当天订单各种商品的总重量
     *
     * @return
     */
    @role(role = 20)
    @ResponseBody
    @GetMapping("/viewWeightByAllGoods")
    public Result viewWeightByAllGoods(HttpServletRequest httpServletRequest) {
        final List<GoodsVo> list = adminService.viewWeightByAllGoods();
        return Result.ok("查看成功").setData(list);
    }


    /**
     * 打印当天的订单
     *
     * @return
     */
    @role(role = 21)
    @ResponseBody
    @GetMapping("/viewOrderByDay")
    public Result viewOrderByDay(HttpServletRequest httpServletRequest,Integer num,Integer page) {
        if (page == null || num == null){
            return Result.error("给我传空我打死你");
        }
        if (page <=0 ){
            return Result.error("页数不能小于1");
        }
        final List<OrdersVo> ordersVoList = adminService.viewOrderByDay(num,page);
        final Integer integer = orderService.vieOrderDayCount();
        Map<String,Object> map = new HashMap<>();
        map.put("total",integer);
        map.put("message",ordersVoList);
        return Result.ok("查看成功").setData(map);
    }


    /**
     * 增加一进货纪录
     * @return
     */
    @role(role = 22)
    @ResponseBody
    @PostMapping("/addStockRecord")
    public Result addStockRecord(HttpServletRequest httpServletRequest,@RequestBody StockDto stockDto){
        if (stockDto == null){
            return Result.error("数据不能为空");
        }
        if (adminService.addStockRecord(stockDto,stockDto.getStockDetailsDtos())) {
            return Result.ok("添加成功");
        }
        return Result.error("添加失败");

    }


    /**
     * 查看进货纪录
     * @param page
     * @param num
     * @return
     */
    @role(role = 23)
    @ResponseBody
    @GetMapping("/viewAllStock")
    public Result viewAllStock(HttpServletRequest httpServletRequest,Integer page,Integer num,@RequestParam(defaultValue = "2020/01/01") Date qDate,@RequestParam(defaultValue = "3000/01/10") Date hDate){
        if (page == null || num == null){
            return Result.error("数据不能为空");
        }
        if (page <= 0){
            return Result.error("页数不能小于1");
        }
        final List<StockVo> list = adminService.viewAllStock(num, page,qDate,hDate);
        if (list == null){
            return Result.error("查看失败");
        }
        final Integer integer = stockService.viewCount();
        Map<String,Object> map = new HashMap<>();
        map.put("total",integer);
        map.put("message",list);
        return Result.ok("查看成功").setData(map);
    }


    /**
     * 根据时间类型查看
     * @param page
     * @param num
     * @param state -1全部  0送达小店 1未送达小店  2已收货 3已分拣 4已下单未支付 5确任收货 6已付款未分拣
     * @param qDate
     * @param hDate
     * @return
     */
    @role(role = 24)
    @ResponseBody
    @GetMapping("/viewAllOrderByTimeAndState")
    public Result viewAllOrderByTimeAndState(HttpServletRequest httpServletRequest,Integer page,Integer num,Integer state,Date qDate,Date hDate){
        if (state == null || page == null || qDate == null || hDate == null || num == null || page <= 0 || num <0){
            return Result.error("数据不合法");
        }
        if (state < -2 || state > 6){
            return Result.error("类型不存在");
        }
        return Result.ok("查看成功").setData(adminService.viewAllOrderByTimeAndState(page,num,qDate,hDate,state));
    }


    /**
     * 查看所有用户
     * @param httpServletRequest
     * @param page
     * @param num
     * @return
     */
    @role(role = 25)
    @ResponseBody
    @GetMapping("/viewAllUser")
    public Result viewAllUser(HttpServletRequest httpServletRequest,Integer page,Integer num){
        if (page == null || num == null || page <= 0 || num < 0){
            return Result.error("参数异常");
        }
        final Map<String, Object> map = adminService.viewAllUser(page, num);
        Map<String,Object> user = map;
        return Result.ok("查看成功").setData(map);
    }


    /**
     * 查看用户流水
     * @param httpServletRequest
     * @param userId
     * @param page
     * @param num
     * @param state
     * @param qDate
     * @param hDate
     * @return
     */
    @role(role = 26)
    @ResponseBody
    @GetMapping("/viewUserCostRecord")
    public Result viewUserCostRecord(HttpServletRequest httpServletRequest,Long userId,Integer page,Integer num,Integer state,Date qDate,Date hDate){
        if (userId == null || page == null || page <= 0 || num == null || num < 0 || state == null || qDate == null || hDate == null){
            return Result.error("数据不能为空");
        }
        final Map<String, Object> map = userService.viewCostRecord(userId, state, page, num, qDate, hDate);
        return Result.ok("查看成功").setData(map);
    }

    /**
     * 查看管理员信息
     * @param httpServletRequest
     * @return
     */
    @ResponseBody
    @GetMapping("/viewAdminMessage")
    public Result viewAdminMessage(HttpServletRequest httpServletRequest){
        final Object id = httpServletRequest.getAttribute("id");
        final int i = Integer.parseInt(id + "");
        final AdminVo adminVo = adminService.viewAdminMessage(i);
        if (adminVo == null){
            return Result.error("身份不存在");
        }
        return Result.ok("查看成功").setData(adminVo);
    }

    /**
     * 根据店家id查看店家流水
     * @param httpServletRequest
     * @param storeId
     * @return
     */
    @role(role = 28)
    @ResponseBody
    @GetMapping("/viewStoreCostRecordByStoreId")
    public Result viewStoreCostRecordByStoreId(HttpServletRequest httpServletRequest,Long storeId,Integer page,Integer num){
        if (storeId == null || page == null || num == null || page <= 0 || num < 0){
            return Result.error("参数不能为空,或参数不能为空");
        }
        final List<StoreCostRecordVo> storeCostRecordVos = storeService.viewAllStoreCostRecord(storeId, page, num);
        final Integer integer = storeService.viewCostRecordCount(storeId);
        Map<String,Object> map = new HashMap<>();
        map.put("total",integer);
        map.put("message",storeCostRecordVos);
        return Result.ok("查看成功").setData(map);

    }

    /**
     * 按时间和类型查看流水纪录
     * @param httpServletRequest
     * @param qDate
     * @param hDate
     * @param page
     * @param num
     * @param state -1 全部，0 提现，1拉人，2取货
     * @return
     */
    @role(role = 29)
    @ResponseBody
    @GetMapping("/viewStoreRecordByTimeAndState")
    public Result viewStoreRecordByTimeAndState(HttpServletRequest httpServletRequest, Date qDate, Date hDate, Integer page, Integer num, Integer state,Integer storeId){
        if (qDate == null || hDate == null || num == null || page == null || storeId == null){
            return Result.error("数据不能为空");

        }
        if (page <= 0){
            return Result.error("页数不能小于等于0");
        }
        final List<StoreCostRecordVo> storeCostRecordVos = storeService.viewStoreRecordByTimeAndState(storeId, qDate, hDate, page, num, state);
        Map<String,Object> map = new HashMap<>();
        map.put("total",storeCostRecordService.viewCountByTime(storeId,qDate,hDate,state));
        map.put("data",storeCostRecordVos);
        return Result.ok("查看成功").setData(map);
    }


    /**
     * 查看分拣纪录
     * @param httpServletRequest
     * @param qDate
     * @param hDate
     * @param page
     * @param num
     * @param adminId
     * @param goodsId
     * @return
     */
    @role(role = 30)
    @ResponseBody
    @GetMapping("/viewSortRecord")
    public Result viewSortRecord(HttpServletRequest httpServletRequest,@RequestParam(defaultValue = "2000/01/01") Date qDate, @RequestParam(defaultValue = "3000/01/10")Date hDate, Integer page, Integer num, @RequestParam(defaultValue = "0") Integer adminId,  @RequestParam(defaultValue = "0")Long goodsId){
        if (page == null || num == null || page <= 0 || num < 0){
            return Result.error("参数异常");
        }
        final List<SortVo> sortVos = sortService.viewSortRecord(qDate, hDate, page, num, adminId, goodsId);
        return Result.ok("查看成功").setData(new PageInfo<>(sortVos));
    }


    /**
     *
     * @param page
     * @param num
     * @param qDate
     * @param hDate
     * @param state
     * @return
     */
    @role(role = 31)
    @ResponseBody
    @GetMapping("/viewCost")
    public Result viewCost(HttpServletRequest httpServletRequest,Integer page, Integer num, @RequestParam(defaultValue = "2000/01/01") Date qDate, @RequestParam(defaultValue = "2050/01/10")Date hDate, @RequestParam(defaultValue = "-1") Integer state){
        if (page == null || num == null || page <= 0 || num < 0){
            return Result.error("参数异常");
        }
        final List<AdminCostRecordVo> adminCostRecordVos = adminCostRecordService.viewCost(page, num, qDate, hDate, state);
        Map<String,Object> map = new HashMap<>();
        map.put("message",new PageInfo<>(adminCostRecordVos));
        map.put("stockMoney",adminCostRecordService.viewBillByState(0,qDate,hDate));
        map.put("returnMoney",adminCostRecordService.viewBillByState(3,qDate,hDate));
        map.put("orderMoney",adminCostRecordService.viewBillByState(2,qDate,hDate));
        map.put("tiXianMoney",adminCostRecordService.viewBillByState(1,qDate,hDate));
        return Result.ok("查看成功").setData(map);
    }
}
